<?php
$head="	
<link rel=\"stylesheet\" type=\"text/css\" href=\"../design/css/devices.css\">
<script type=\"text/javascript\" src=\"../javascript/pagesJavascript/devices.js\"></script>
<script type=\"text/javascript\">	
$(document).ready(function() { 
    $(\"#devicesList\").tablesorter({ 
        // pass the headers argument and assing a object 
        headers: { 
            // assign the secound column (we start counting zero)
            0: { 
                // disable it by setting the property sorter to false 
                sorter: false 
            }, 
            1: { 
                // disable it by setting the property sorter to false 
                sorter: false 
            }, 
            10: { 
                // disable it by setting the property sorter to false 
                sorter: false 
            }, 
            // assign the third column (we start counting zero) 
            11: { 
                // disable it by setting the property sorter to false 
                sorter: false 
            } 
        } 
    }); 
});
</script>";
$title = "Alle Ger&auml;te";
require_once '../classes/includeAll.php';
require_once '../menu/head.php';
require '../resources/getConnection.php';     
$devicesClass = new Devices($connection);

$selection = true; 

if(isset($_GET['d']) && $selection)     //Get DeviceType ID if isset
{
    $selection = false;                 //Disable the other selection
    $devTyp = $_GET['d'];               //get Device Type ID
    if(in_array($devTyp, $devicesClass->DeviceTypesID))
    {                                   //Set the Title of the Homepage
       $title= "Ger&auml;te vom Typ " . $devicesClass->DeviceTypes[array_search($devTyp, $devicesClass->DeviceTypesID)];
       $devices = $devicesClass->getAllofDeviceType($devTyp); //get the devices of the given deviceType
    }else
    {
       $devices =  $devicesClass->getAllDevices(); 
    }   
}

if(isset($_GET['room']) && $selection)
{
    $selection = false;
    $roomsID = array(); 
    $roomGET = $_GET['room']; 
    $roomAndLevelClass = new roomAndLevel($connection);
    $roomsPerLevels = array();
    $levels = $roomAndLevelClass->getAllLevel();
    foreach ($levels as $level) 
    {      
        $roomsPerLevels = $roomAndLevelClass->getAllRoomsPerLevel($level['ID']);
        foreach ($roomsPerLevels as $room) 
        {
            array_push($roomsID, $room['ID']);
        } 
    }
    if(in_array($roomGET, $roomsID))
    {
        
        $rooms = $roomAndLevelClass->GetAllRoom();
        foreach ($rooms as $room) 
        {
            if($room['ID'] == $roomGET)
            {
                $title = "Alle Ger&auml;te aus " . $room['name'];
            }
        }   
        
        $devices = $devicesClass->getAllofRoom($roomGET);
    }
    else
    {
       $devices =  $devicesClass->getAllDevices(); 
    }
}

if(isset($_GET['level']) && $selection)
{
    $selection = false;
    $levelGET = $_GET['level'];
    $roomAndLevelClass = new roomAndLevel($connection);
    $levelsElement = $roomAndLevelClass->getAllLevel();
    $levels = array();
    foreach ($levelsElement as $level) 
    {
        array_push($levels, $level['ID']);
    }
    if(in_array($levelGET, $levels))
    {
        foreach ($levelsElement as $level) 
        {
            if($level['ID']==$levelGET)     
            {
                $title = "Alle Ger&auml;te vom " . $level['name'];
            }
        }
       $devices = $devicesClass->getAllofLevel($levelGET, $roomAndLevelClass); 
    }else
    {
        $devices =  $devicesClass->getAllDevices();   
    }
}

if($selection)
{
    $devices =  $devicesClass->getAllDevices(); 
}
echo"
    
<h1>$title</h1>
<div id=\"filter\">
    <div id=\"filterh2\">
        <h2>Filter:</h2>
    </div>
    <div id=\"radioButtons\">
        <input type=\"radio\" name=\"select\" value=\"name\" id=\"selectName\" checked=\"checked\"/> Name:<br>
        <input type=\"radio\" name=\"select\" value=\"invNumber\"  id=\"selectInvNumber\"/> Inventar Nummer:
    </div>
    <div id=\"searchField\">
        <input type=\"text\" name=\"searchInput\" value=\"\" id=\"searchInput\" />
    </div>
    <div id=\"load\"></div>
     <div id=\"button\"><input type=\"button\" value=\"Ausgew&auml;hlte l&ouml;schen\" name=\"search\" id=\"delete\" /></div>
</div>
<form id=\"deleteChecked\">

<table id=\"devicesList\" >
    <thead>
        <tr>
            <th><input type=\"checkbox\" name=\"checkAllDevices\" value=all\" id=\"checkAllDevices\" /></th>
            <th></th>
            <th>Nr.</th>
            <th>Name</th>
            <th>Typ</th>
            <th>Inventar Nr.</th>
            <th>Stockwerk</th>
            <th>Raum </th>
            <th>Raum Nr.</th>
            <th>Raum Soll</th>
            <th>Warnung</th>
            <th></th>
        </tr>
    </thead>
";
    $i=1;
foreach ($devices as $device) 
    { 
        $device->getInfrastructure(); 
        $device->getAdditions();
//        $lastScan ="" ;
//        if(isset($device->RelocationDate[0]))
//        {
//            $lastScan = ConvertDate($device->RelocationDate[0], true);   
//        }
        
$flagMeaning = (new allFlags())->DeviceFlags;
$alertImg = array();
foreach ($flagMeaning as $key => $value) 
{    
    if(($device->ChangeFlag & $value)>0)
    {
        $tempImg = alertLevel($key);
        if(!in_array($tempImg, $alertImg))
        {        
                array_push($alertImg, $tempImg);
        } 
    }
}
$locationError = 0;
$locationErrorString = '';
if($device->RoomIs == null)
{
    $locationError |= 0x01;
}
if($device->RoomShould == null)
{
    $locationError |= 0x02;
}
if(($device->RoomShould != null) &&($device->RoomIs != null))
{
    if(($device->RoomIs[0] != $device->RoomShould[0]) )
    {
        $locationError |= 0x04;
    }
}  
switch($locationError)
{
    case 0x01:
    case 0x02:
        $locationErrorString = '<img width="16px" src="../design/pictures/questionmarkred.png">';
    break;

    case 0x04:
        $locationErrorString = '<img width="16px" src="../design/pictures/exclamation.png">';
    break;

    default:
    break;
}
$changeFlag = "";
foreach ($alertImg as $value) 
{
    $changeFlag .= $value;   
}
      echo"    
            <tr deviceID=\"$device->ID\" >
                <td><input type=\"checkbox\" name=\"del[$device->ID]\" value=\"\" class=\"deleteCheckbox\" /></td>
                <td><a href=\"device.php?dev=". $device->ID."\" >".$locationErrorString."</a></td>
                <td><a href=\"device.php?dev=". $device->ID."\" >".$i."</a></td>
                <td><a href=\"device.php?dev=". $device->ID."\" >". sanitizeOutputString($device->Name) ."</a></td>
                <td><a href=\"device.php?dev=". $device->ID."\" >". sanitizeOutputString($device->DeviceType) ."</a></td>
                <td><a href=\"device.php?dev=". $device->ID."\" >". sanitizeOutputString($device->InventoryNumber) ."</td>
                <td><a href=\"device.php?dev=". $device->ID."\" >"; if($device->LevelIs != null){echo sanitizeOutputString($device->LevelIs[0]); } echo"</td>
                <td><a href=\"device.php?dev=". $device->ID."\" >"; if($device->RoomIs != null){echo sanitizeOutputString($device->RoomIs[0]); } echo "</td>
                <td><a href=\"device.php?dev=". $device->ID."\" >"; if($device->RoomIsNumber != null){echo sanitizeOutputString($device->RoomIsNumber[0]); } echo"</td>
                <td><a href=\"device.php?dev=". $device->ID."\" >"; if($device->RoomShould != null){echo sanitizeOutputString($device->RoomShould[0]);}  echo"</td>
                <td><a href=\"device.php?dev=". $device->ID."\" >". $changeFlag . "</td>
                <td onclick=\"deleteDevice(this, ". $device->ID .")\" ><img width=\"16px\" src=\"../design/pictures/delete.png\"></a>  </td> 
            </tr>
            "; 
      $i++;       
    }
       echo"</tbody></table></form>"; 
    $connection->disconnectDB();    
require_once '../menu/footer.php';  //Design Part 2
?>

